local skel = fk.CreateSkill {
  name = "emo__lishou",
}

Fk:loadTranslationTable{
  ["emo__lishou"] = "骊首",
  [":emo__lishou"] = "游戏开始时，你令所有角色依次将牌堆顶两张牌分别当【+1马】、【-1马】使用。你可以使用或打出场上转化马的底牌（无距离次数限制），并令以此法失去牌的其他角色本回合非锁定技失效，受到伤害+1且改为雷电伤害。",

  ["#emo__lishou"] = "骊首：你可以使用场上的“骊首”转化马！",
  ["#emo__lishou-use"] = "骊首：使用来自“%src”的【%arg】",
  ["@@emo__lishou-turn"] = "被骊首",

  ["$emo__lishou1"] = "（马叫）",
  ["$emo__lishou2"] = "（马叫）",
}

local getHorses = function ()
  local ids = {}
  for _, p in ipairs(Fk:currentRoom().alive_players) do
    for _, c in ipairs(table.connect(p:getEquipCards(Card.SubtypeOffensiveRide), p:getEquipCards(Card.SubtypeDefensiveRide))) do
      local id = c:getEffectiveId()
      if id ~= nil and c.name ~= Fk:getCardById(id, true).name then
        table.insertIfNeed(ids, id)
      end
    end
  end
  return ids
end

skel:addEffect("viewas", {
  anim_type = "offensive",
  prompt = function (self, player, selected_cards, selected)
    local cid = selected_cards[1]
    if cid then
      local owner = Fk:currentRoom():getCardOwner(cid)
      if owner then
        return "#emo__lishou-use:" .. owner.id .. "::" .. Fk:getCardById(cid, true).name
      end
    end
    return "#emo__lishou"
  end,
  pattern = ".",
  expand_pile = function (self, player)
    return table.filter(getHorses(), function (id)
      return not table.contains(player:getCardIds("e"), id) -- 别展开自己的装备区……
    end)
  end,
  card_filter = function (self, player, to_select, selected)
    if #selected == 0 and table.contains(getHorses(), to_select) then
      if Fk.currentResponsePattern then
        return Exppattern:Parse(Fk.currentResponsePattern):match(Fk:getCardById(to_select, true))
      else
        return player:canUse(Fk:getCardById(to_select, true))
      end
    end
  end,
  view_as = function (self, player, cards)
    if #cards ~= 1 then return nil end
    return Fk:getCardById(cards[1], true)
  end,
  before_use = function(self, player, use)
    use.extraUse = true
    local room = player.room
    local cid = use.card.id
    if not cid or cid == 0 then return end
    local owner = room:getCardOwner(cid)
    if owner and owner ~= player then
      room:doIndicate(player, {owner})
      room:addPlayerMark(owner, MarkEnum.UncompulsoryInvalidity .. "-turn")
      room:setPlayerMark(owner, "@@emo__lishou-turn", cid)
    end
  end,
  enabled_at_play = function(self, player)
    return #getHorses() > 0
  end,
  enabled_at_response = function (self, player, response)
    if Fk.currentResponsePattern then
      for _, id in ipairs(getHorses()) do
        if Exppattern:Parse(Fk.currentResponsePattern):match(Fk:getCardById(id, true)) then
          return true
        end
      end
    end
  end,
})

skel:addEffect(fk.GameStart, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:doIndicate(player, room.alive_players)
    local temp = player
    local players = {}  ---@type ServerPlayer[] -- 从自己开始
    repeat
      if not temp.dead then
        table.insert(players, temp)
      end
      temp = temp.next
    until temp == player
    local off_horse, def_horse = {"chitu"}, {"jueying"}
    for _, id in ipairs(room.draw_pile) do
      local card = Fk:getCardById(id)
      if card.sub_type == Card.SubtypeOffensiveRide then
        table.insertIfNeed(off_horse, card.name)
      elseif card.sub_type == Card.SubtypeDefensiveRide then
        table.insertIfNeed(def_horse, card.name)
      end
    end
    for _, p in ipairs(players) do
      for _, names in ipairs({off_horse, def_horse}) do
        if not p.dead then
          local name = table.random(names)
          local card = Fk:cloneCard(name)
          card.skillName = skel.name
          card:addSubcard(room:getNCards(1)[1])
          if p:canUseTo(card, p) then
            room:useCard{
              from = p, tos = {p}, card = card,
            }
            room:delay(250)
          end
        end
      end
    end
  end,
})

skel:addEffect(fk.DamageInflicted, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:getMark("@@emo__lishou-turn") > 0
  end,
  on_use = function(self, event, target, player, data)
    data:changeDamage(1)
    data.damageType = fk.ThunderDamage
  end,
})

skel:addEffect("targetmod", {
  bypass_distances = function (self, player, skill, card, to)
    return card and player:hasSkill(skel.name) and table.contains(getHorses(), card:getEffectiveId())
  end,
  bypass_times = function (self, player, skill, scope, card, to)
    return card and player:hasSkill(skel.name) and table.contains(getHorses(), card:getEffectiveId())
  end,
})

return skel
